' 该文档已过时, 其功能已经被集成到 NCB612DTService 服务中. 此文件以后不在使用'
Imports System.Xml
Imports System.Xml.Linq

''' <summary>
''' 南兴加工文件
''' </summary>
Public Class NXXmlService

#Region " PanelEntity 转 XDocument"



    ''' <summary>
    ''' 保存对象
    ''' </summary>
    ''' <param name="xdoc"></param>
    ''' <param name="path"></param>
    Public Sub save(ByRef xdoc As XDocument, ByRef path As String)
        xdoc.Save(path)
    End Sub

    ''' <summary>
    ''' 构建xml文档
    ''' </summary>
    ''' <param name="p"> PanelEntity对象 </param>
    ''' <returns></returns>
    Public Function buildDoc(ByRef p As PanelEntity) As XDocument


        '构建加工组
        Dim machines As XElement = buildMachines()
        For Each item In p.Machines
            machines.Add(buildMachining(item))
        Next

        '构建封边组
        Dim edgegroup As XElement = buildEdgeGroup()
        Dim edge As XElement
        For i = 1 To 4
            edge = biuldEdge(i)
            edgegroup.Add(edge)
        Next

        '构建outline
        Dim outline As XElement = buildOutline(p.lenght, p.width)

        '构建工件
        Dim panel As XElement = buildPanel(p)
        With panel
            .Add(outline)
            .Add(machines)
            .Add(edgegroup)
        End With

        '构建文档
        Dim doc As XDocument = buildDoc()
        doc.Element("Root").Element("Project").Element("Panels").Add(panel)

        Return doc

    End Function

    ''' <summary>
    ''' 构建文档
    ''' </summary>
    Private Function buildDoc() As XDocument
        Dim doc As XDocument = New XDocument(New XDeclaration("1.0", "utf-8", "yes"),
                                              New XElement("Root", New XAttribute("Cad", "BuiltInCad"), New XAttribute("version", "2.0"),
                                                           New XElement("Project",
                                                                        New XElement("Panels"))))
        Return doc
    End Function

    Private Function buildPanel(ByRef p As PanelEntity) As XElement
        Dim panel As XElement = New XElement("Panel")

        If Not p.isProduce Is Nothing Then panel.Add(New XAttribute("IsProduce", p.isProduce.ToString))
        If Not p.id Is Nothing Then panel.Add(New XAttribute("ID", p.handle))
        If Not p.name Is Nothing Then panel.Add(New XAttribute("Name", p.name))
        If Not p.width Is Nothing Then panel.Add(New XAttribute("Width", p.width))
        If Not p.lenght Is Nothing Then panel.Add(New XAttribute("Length", p.lenght))
        If Not p.thickness Is Nothing Then panel.Add(New XAttribute("Thickness", p.thickness))
        If Not p.machiningPoint Is Nothing Then panel.Add(New XAttribute("MachiningPoint", p.machiningPoint))

        Return panel
    End Function


    Private Function buildOutline(ByVal x As Double, ByVal y As Double) As XElement
        Dim outline As New XElement("Outline",
                                    New XElement("Point", New XAttribute("X", x), New XAttribute("Y", y)),
                                    New XElement("Point", New XAttribute("X", 0), New XAttribute("Y", y)),
                                    New XElement("Point", New XAttribute("X", 0), New XAttribute("Y", 0)),
                                    New XElement("Point", New XAttribute("X", x), New XAttribute("Y", 0))
                                    )
        Return outline
    End Function

    Private Function buildMachines() As XElement
        Dim machines As New XElement("Machines")
        Return machines
    End Function

    Private Function buildMachining(ByRef m As MachiningEntity) As XElement
        Dim machine As New XElement("Machining")

        If Not m.type Is Nothing Then machine.Add(New XAttribute("Type", m.type))
        If Not m.isGenCode Is Nothing Then machine.Add(New XAttribute("IsGenCode", m.isGenCode))
        If Not m.face Is Nothing Then machine.Add(New XAttribute("Face", m.face))
        If Not m.X Is Nothing Then machine.Add(New XAttribute("X", m.X))
        If Not m.Y Is Nothing Then machine.Add(New XAttribute("Y", m.Y))
        If Not m.Z Is Nothing Then machine.Add(New XAttribute("Z", m.Z))

        If Not m.diameter Is Nothing Then machine.Add(New XAttribute("Diameter", m.diameter))
        If Not m.depth Is Nothing Then machine.Add(New XAttribute("Depth", m.depth))
        If Not m.direction Is Nothing Then machine.Add(New XAttribute("Direction", m.direction))

        If Not m.EndX Is Nothing Then machine.Add(New XAttribute("EndX", m.EndX))
        If Not m.EndY Is Nothing Then machine.Add(New XAttribute("EndY", m.EndY))
        If Not m.EndZ Is Nothing Then machine.Add(New XAttribute("EndZ", m.EndZ))
        If Not m.width Is Nothing Then machine.Add(New XAttribute("Width", m.width))
        If Not m.toolOffset Is Nothing Then machine.Add(New XAttribute("ToolOffset", m.toolOffset))

        If Not m.maxDepth Is Nothing Then machine.Add(New XAttribute("MaxDepth", m.maxDepth))
        If Not m.length Is Nothing Then machine.Add(New XAttribute("Length", m.length))


        Return machine
    End Function


    Private Function buildEdgeGroup() As XElement
        Dim edgeGroup As New XElement("EdgeGroup")
        edgeGroup.Add(New XAttribute("X1", "0"))
        edgeGroup.Add(New XAttribute("Y1", "0"))
        Return edgeGroup
    End Function


    Private Function biuldEdge(ByVal face As Integer) As XElement
        Dim edge As New XElement("Edge",
                                 New XAttribute("Face", face),
                                 New XAttribute("Thickness", "0"),
                                 New XAttribute("Pre_Milling", "0"),
                                 New XAttribute("X", "0"),
                                 New XAttribute("Y", "0"),
                                 New XAttribute("CentralAngle", "0"))
        Return edge
    End Function


#End Region



End Class
